package com.mo.service.impl;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mo.config.listener.UploadDataListener;
import com.mo.constant.Constants;
import com.mo.mapper.TCluemapper;
import com.mo.model.TClue;
import com.mo.model.TUser;
import com.mo.query.BaseQuery;
import com.mo.query.ClueQuery;
import com.mo.service.ClueService;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
 * @author mo
 * version
 */
@Service
public class ClueServiceImpl implements ClueService {
    @Resource
    TCluemapper tCluemapper;
    @Override
    public PageInfo<TClue> getClueByPage(Integer current) {
        //设置PageHelper
        PageHelper.startPage(current, Constants.PAGE_SIZE);
        //查询
        List<TClue> list = tCluemapper.selectClueByPage(BaseQuery.builder().build());
        PageInfo<TClue> info = new PageInfo<>(list);
        return info;
    }

    @Override
    public void importExcel(InputStream inputStream, String token) {
        //三个参数 1.要读取的Excel文件(输入的方式) 2.Excel模板类 3.文件读取的监听器
        EasyExcel.read(inputStream, TClue.class,  new UploadDataListener(tCluemapper,token)).sheet().doRead();
    }

    @Override
    public Boolean checkPhone(String phone) {
        int count = tCluemapper.selectByCount(phone);
        return count<=0; //手机号没有查到就是true
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public synchronized int saveClue(ClueQuery clueQuery) {
        int count =tCluemapper.selectByCount(clueQuery.getPhone());
        if (count<=0){
            TClue tClue = new TClue();
            BeanUtils.copyProperties(clueQuery,tClue);
            TUser user = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
            tClue.setCreateTime(new Date());
            tClue.setCreateBy(user.getId());
            return tCluemapper.insertSelective(tClue);
        }else {
            throw new RuntimeException("该手机号已经录入过了，不能再录入");
        }

    }

    @Override
    public TClue getClueById(Integer id) {
        return tCluemapper.selectDetailById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateClue(ClueQuery clueQuery) {
        TClue clue = new TClue();
        BeanUtils.copyProperties(clueQuery, clue);
        clue.setEditTime(new Date());
        //security的安全上下文获取
        TUser user = (TUser) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        clue.setEditBy(user.getId());
        return tCluemapper.updateByPrimaryKeySelective(clue);
    }

    @Override
    public int deleteClueById(Integer id) {
       return tCluemapper.deleteByPrimaryKey(id);
    }

    @Override
    public int batchDelClueByIds(List<String> idList) {
        return tCluemapper.deleteByIds(idList);
    }
}
